<?php


namespace api\controller;


use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TenantUserService;
use think\Request;
use think\Response;

class TenantUserController extends VerifyController
{
    /**
     * @var TenantUserService
     */
    protected $tenantUserService;


    public function __construct()
    {
        parent::__construct();
        $this->tenantUserService = new TenantUserService();
    }

    /**
     * @param Request $request
     * @return Response
     */
    public function selectTenantUser(Request $request)
    {
        $param = $request->param();
        $includeEmployed = $param['param']['include_employed'] ?? 'no';
        $res = $this->tenantUserService->selectTenantUser($param['param'], $includeEmployed === 'yes');
        return json(success_response('OK', $res));
    }

    /**
     * 公司成员二级树
     * @param Request $request
     * @return Response
     */
    public function tree(Request $request)
    {
        $includeEmployed = $request->param()['param']['include_employed'] ?? 'no';
        $res = $this->tenantUserService->tree($request->getTenantId(), $includeEmployed === 'yes');
        return json(success_response('OK', $res));
    }

    /**
     * 设置角色
     * @param Request $request
     * @return Response|void
     * @throws \Throwable
     */
    public function setRoles(Request $request)
    {
        try {
            $param = $request->param();
            $param['data']['role_id'] = $param['data']['role_id'] ?? [];
            $resData = $this->tenantUserService->setRoles($param['data']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 设置部门
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function setDepartments(Request $request)
    {
        try {
            $param = $request->param();
            $param['data']['department_id'] = $param['data']['department_id'] ?? [];
            $resData = $this->tenantUserService->setDepartments($param['data']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 租户成员标记离职
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function depart(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $resData = $this->tenantUserService->depart($filter);
        return json(success_response('', $resData));
    }

    /**
     * 移除离职状态
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function entry(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $resData = $this->tenantUserService->entry($filter);
        return json(success_response('', $resData));
    }
}
